package com.xqls.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.xqls.bean.AdministrationBean;
import com.xqls.biz.AdministrationBiz;
import com.xqls.util.CommonPage;
import com.xqls.util.CommonResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping(value = "/administration")
public class AdministrationController {

	@Autowired
	private AdministrationBiz administrationBiz;

	@RequestMapping(value = "/administrationList")
	public ModelAndView administrationList() {
		ModelAndView view = new ModelAndView("administration/administrationList");
		view.addObject("stringUtils", new StringUtils());
		return view;
	}

	@RequestMapping(value = "/administrationListData")
	@ResponseBody
	public CommonResult administrationListData(@RequestBody CommonPage commonPage, HttpServletRequest request, HttpServletResponse response) {
		CommonResult result = administrationBiz.queryAdministration(commonPage,request,response);
		if (result.getIsSuccess()) {
			result.setData(commonPage);
		}
		return result;
	}

	@RequestMapping(value = "/maintainAdministration/{tableno}")
	public ModelAndView maintainAdministration(@PathVariable(value = "tableno") String tableno) {
		ModelAndView view = new ModelAndView("administration/maintainAdministration");
		AdministrationBean administrationBean = administrationBiz.queryAdministratiobByTableno(tableno);

		String[] display = administrationBean.getDisplayfield().split(",");// 显示字段
		String[] hidden = administrationBean.getHiddenfield().split(",");// 所有字段
		List<String> fields = new ArrayList<String>();
		List<String> hiddenfields = new ArrayList<String>();
		if (display != null) {
			for (int i = 0; i < display.length; i++) {
				fields.add(display[i]);

			}
		}

		for (int i = 0; i < hidden.length; i++) {
			hiddenfields.add(hidden[i]);
		}
		view.addObject("alldisplay", fields);
		view.addObject("allfields", hiddenfields);
		view.addObject("administration", administrationBiz.queryAdministratiobByTableno(tableno));
		return view;
	}

	@RequestMapping(value = "/save")
	@ResponseBody
	public String administrationSave(AdministrationBean bean) {
		CommonResult result = administrationBiz.updateAdministration(bean);

		return JSONObject.toJSONString(result);

	}

	@RequestMapping(value = "administrationEdit/{tableno}")
	public ModelAndView realdataEdit(@PathVariable(value = "tableno") String tableno) {
		ModelAndView view = new ModelAndView("administration/administrationEdit");
		view.addObject("administration", administrationBiz.queryAdministratiobByTableno(tableno));
		return view;
	}

	@RequestMapping(value = "/saveFiled")
	@ResponseBody
	public CommonResult Save(AdministrationBean bean, String filed, String fieldalias, String type, int isdisplay) {
		String str = bean.getTablename();
		System.out.println(bean.getFieldtype());
		String display = null;
		CommonResult result = null;
		result = administrationBiz.addFiled(str, filed, type);
			display = "," + fieldalias +":"+ filed;
			

		AdministrationBean beans = new AdministrationBean();
		beans.setTableno(bean.getTableno());
		beans.setTablename(bean.getTablename());
		beans.setHiddenfield(bean.getHiddenfield() + display);
		beans.setTablealias(bean.getTablealias());
		beans.setFieldtype(bean.getFieldtype()+","+filed+":"+type);
		if (isdisplay == 1) {
			beans.setDisplayfield(bean.getDisplayfield() + display);
		} else {
			beans.setDisplayfield(bean.getDisplayfield());
		}

		result = administrationBiz.updateAdministration(beans);
		return result;

	}

}
